#include <bits/stdc++.h>

#define in read()
#define fi first
#define se second
#define pii pair<int,int>
#define pb push_back
#define vec vector<int>
#define y1 y_____hahahaha_____1

using namespace std;

typedef long long ll;
typedef double db;
typedef unsigned long long ull;
typedef long double ldb;

int read(){int x = 0,sgn = 1;char ch = getchar();for(;!isdigit(ch);ch = getchar()) if(ch == '-') sgn = -1;for(;isdigit(ch);ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ 48); return x * sgn;}

const int N = 2e5+10;
const int LL = 2e7 + 10;

int ch[LL][2],s[LL],rt[N],nodenum;
int a[N],n,m;

void insert(int &y,int x,int v,int l = 0,int r = n){
	y = ++nodenum; ch[y][0] = ch[x][0]; ch[y][1] = ch[x][1];  s[y] = s[x] + 1; if(l == r) return; int mid = l + r >> 1;
	if(v <= mid) insert(ch[y][0],ch[y][0],v,l,mid); else if(v > mid) insert(ch[y][1],ch[x][1],v,mid + 1,r);
}

int query(int y,int x,int xl,int xr,int l = 0,int r = n){
	if(!y) return 0; if(xl == l && xr == r) return s[y] - s[x]; int mid = l + r >> 1;
	if(xr <= mid) return query(ch[y][0],ch[x][0],xl,xr,l,mid); if(xl > mid) return query(ch[y][1],ch[x][1],xl,xr,mid + 1,r);
	else return query(ch[y][0],ch[x][0],xl,mid,l,mid) | query(ch[y][1],ch[x][1],mid + 1,xr,mid + 1,r);
}

int qry(int l,int r,int xl,int xr){return xl > xr ? 0 : query(rt[r],rt[l-1],max(xl,0),min(xr,n));}

int main(){
#ifndef ONLINE_JUDGE
	freopen("1.in","r",stdin);
#endif
	n = in,m = in; for(int i = 1;i <= n;i++) a[i] = in,insert(rt[i],rt[i-1],a[i]);
	while(m--){
		int b = in,x = in,l = in,r = in,ans = 0;
		for(int i = 18;i >= 0;i--){
			int nxt = ans; if(!(b >> i & 1)) nxt |= 1 << i;
			if(qry(l,r,nxt - x,nxt + (1 << i) - 1 - x)) ans = nxt;
			else ans |= (b >> i & 1) << i;
		}
		printf("%d\n",ans ^ b);
	}
	return 0;
}
